<template>
  <div class="image-container" :style="imageContainerStyle">
    <el-image
      :src="`${realSrc}`"
      :fit="props.fit"
      :preview-src-list="realSrcList"
      loading="lazy"
      preview-teleported>
      <template #error>
        <div class="image-slot">
          <el-icon><picture-filled /></el-icon>
        </div>
      </template>
    </el-image>
  </div>
</template>

<script setup>
import { isExternal } from "@/utils/validate";

const props = defineProps({
  src: {
    type: String,
    default: ""
  },
  previewSrcList: {
    type: Array,
    default: () => []
  },
  width: {
    type: [Number, String],
    default: ""
  },
  height: {
    type: [Number, String],
    default: ""
  },
  fit: {
    type: String,
    default: ''
  }
});

const realSrc = computed(() => {
  if (!props.src) {
    return;
  }
  let real_src = props.src.split(",")[0];
  if (isExternal(real_src)) {
    return real_src;
  }
  return import.meta.env.VITE_APP_BASE_API + real_src;
});

const realSrcList = computed(() => {
  if (props.previewSrcList.length === 0) {
    return [`${import.meta.env.VITE_APP_BASE_API}${props.src}`]
  } else {
    return props.previewSrcList.map(item => `${import.meta.env.VITE_APP_BASE_API}${item}`)
  }
});

const imageContainerStyle = computed(() => {
  const width = typeof props.width == "string" ? props.width : `${props.width}px`
  const height = typeof props.height == "string" ? props.height : `${props.height}px`

  return {width,height}
})
</script>

<style lang="scss" scoped>
.image-container {
  display: inline-block;
}

.el-image {
  border-radius: 5px;
  background-color: #ebeef5;
  box-shadow: 0 0 5px 1px #ccc;
  :deep(.el-image__inner) {
    transition: all 0.3s;
    cursor: pointer;
    &:hover {
      transform: scale(1.2);
    }
  }
  :deep(.image-slot) {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100%;
    height: 100%;
    color: #909399;
    font-size: 30px;
  }
}
</style>
